from functools import wraps

# 自定义装饰器
from flask import session, current_app,g


def news_hot_filter(index):
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


# 封装用户登录数据
def user_login_data(view_func):
    @wraps(view_func) #不修改原有函数的功能
    def wapper(*args, **kwargs):
        user_id = session.get("user_id")
        user = None
        if user_id:
            try:
                from info.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        #使用g对象保存用户数据
        g.user = user
        return view_func(*args, **kwargs)

    return wapper
